package com.easyadmin.pro.modules.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.easyadmin.pro.common.constants.StatusConstants;
import com.easyadmin.pro.common.enums.HttpCodeEnum;
import com.easyadmin.pro.common.exception.BusinessException;
import com.easyadmin.pro.modules.sys.vo.RoleRelevanceRouterVo;
import com.easyadmin.pro.tool.WebUtils;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.core.util.UpdateEntity;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.easyadmin.pro.modules.sys.entity.UserPermissionsEntity;
import com.easyadmin.pro.modules.sys.mapper.UserPermissionsMapper;
import com.easyadmin.pro.modules.sys.service.IUserPermissionsService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 *  服务层实现。
 *
 * @author xulin
 * @since 2024-04-02
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserPermissionsServiceImpl extends ServiceImpl<UserPermissionsMapper, UserPermissionsEntity> implements IUserPermissionsService {

    private final UserPermissionsMapper userPermissionsMapper;

    @Override
    public void changeRoleRouter(RoleRelevanceRouterVo roleRelevanceRouterVo) {
        Long roleId = roleRelevanceRouterVo.getRoleId();
        if (Objects.isNull(roleId)) {
            throw new BusinessException(HttpCodeEnum.RELEVANCE_ROLE_NULL);
        }
        List<Long> routerIds = roleRelevanceRouterVo.getRouterIds();
        if (CollectionUtil.isEmpty(routerIds)) {
            // 删除角色的所有路由
            UpdateChain.of(UserPermissionsEntity.class)
                    .set(UserPermissionsEntity::getIsDeleted, StatusConstants.STATUS_1)
                    .set(UserPermissionsEntity::getUpdateUserId, WebUtils.getUserId())
                    .set(UserPermissionsEntity::getUpdateTime, LocalDateTime.now())
                    .eq(UserPermissionsEntity::getRoleId, roleId).update();
        } else {
            // 删除不存在现在路由列表的原路由
            UpdateChain.of(UserPermissionsEntity.class)
                    .set(UserPermissionsEntity::getIsDeleted, StatusConstants.STATUS_1)
                    .set(UserPermissionsEntity::getUpdateUserId, WebUtils.getUserId())
                    .set(UserPermissionsEntity::getUpdateTime, LocalDateTime.now())
                    .notIn(UserPermissionsEntity::getRouterId, routerIds)
                    .eq(UserPermissionsEntity::getRoleId, roleId).update();
            List<Long> allRouterIds = listAs(new QueryWrapper().select(UserPermissionsEntity::getRouterId)
                    .eq(UserPermissionsEntity::getRoleId, roleId), Long.class);
            List<UserPermissionsEntity> saveInfos = new ArrayList<>();
            // 添加新路由
            routerIds.forEach(e -> {
                if (!allRouterIds.contains(e)) {
                    UserPermissionsEntity userPermissionsEntity = new UserPermissionsEntity();
                    userPermissionsEntity.setRoleId(roleId).setRouterId(e);
                    saveInfos.add(userPermissionsEntity);
                }
            });
            // 保存新路由
            if (CollectionUtil.isNotEmpty(saveInfos)) {
                this.saveOrUpdateBatch(saveInfos);
            }
        }

    }
}
